# -*- coding: utf-8 -*-
"""
Divides x coordinates in cargomarker xml file by 2

For example
<rect left="566" top="282" right="588" bottom="293"/><text value="DUC3"/><center x="576" y="286" done="1" suspicious="1"/>
becomes
<rect left="283" top="282" right="294" bottom="293"/><text value="DUC3"/><center x="288" y="286" done="1" suspicious="1"/>

Usage:
    python PBAR_ModifyCargoMarker <filename>
where <filename> is the file name including path
Example
    python PBAR_ModifyCargoMarker c:\PBAR-20130828002.cargomarker

The original file is modified and a backup with extention .cargomarkerOLD is created.
The script will refuse to work if the fix has already been completed (there is an 
associated .cargomarkerOLD file present).

Created on Tue Sep 03 10:45:09 2013
@author: john kwong
"""

import codecs
import os
import copy
import shutil
import sys

def PBAR_ModifyCargoMarker(filename):
    
    # Check to see if the file exists
    if not os.path.exists(filename):
        print("File does not exists. Quitting")
        return
    filenameOld = filename.replace('.cargomarker', 'cargomarkerOLD')
    if os.path.exists(filenameOld):
        print("The file has already been converted. Quitting")
        return
    else: # make back up of file
        try:
            shutil.copy(filename, filenameOld)
        except:
            print("Could not copy file")
            return
    # open old file for reading
    with codecs.open(filenameOld, 'rb', encoding = 'utf-16') as fidOld:
        # open file for writting
        with codecs.open(filename, 'wb', encoding = 'utf-16') as fid:
            # Check to see if the file has been convereted already by checking for existence of cargomarkerOLD file.
            for lineIn in fidOld:
                if len(lineIn) < 3:
                    fid.write(lineIn) # the first line seems to contain some whitespace
                else:
                    lineIn2 = copy.copy(lineIn)
                    # Modify the entires
                    # modify left
                    index1 = lineIn2.find('left=') + 6 # index of largest digit
                    index2 = lineIn2[index1:].find('"')+index1 # index parenthesis to the right of the smallest digit
                    theNumber = int(lineIn2[index1:index2]) / 2
                    lineIn2 = u'%s%d%s' %(lineIn2[:index1], theNumber, lineIn2[index2:])
                        
                    # modify right
                    index1 = lineIn2.find('right=') + 7 # index of largest digit
                    index2 = lineIn2[index1:].find('"')+index1 # index parenthesis to the right of the smallest digit
                    theNumber = int(lineIn2[index1:index2]) / 2
                    lineIn2 = u'%s%d%s' %(lineIn2[:index1], theNumber, lineIn2[index2:])
                        
                    # modify center x
                    index1 = lineIn2.find('center x=') + 10 # index of largest digit
                    index2 = lineIn2[index1:].find('"')+index1 # index parenthesis to the right of the smallest digit
                    theNumber = int(lineIn2[index1:index2]) / 2
                    lineIn2 = u'%s%d%s' %(lineIn2[:index1], theNumber, lineIn2[index2:])
                    
                    #write the modified line to the new file
                    fid.write(lineIn2)
    fid.close()
    fidOld.close()
    
    
if __name__ == '__main__':
    PBAR_ModifyCargoMarker(sys.argv[1])
    